﻿/*
 * ──────────────────────────────────────────────────────────────
 *　描   述：
 *　作   者：王浩                                              
 *　版   本：1.0
 *　创建时间：2022/9/8 16:14:24                             
 * ──────────────────────────────────────────────────────────────
 *　命名空间: AllProAutoUpDataClient                                   
 *　类   名: FileDB                                         
 * ──────────────────────────────────────────────────────────────
 */
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AllProAutoUpDataClient
{
    public class FileDB
    {
        /// <summary>
        /// 数据
        /// </summary>
        public FileModel Model { get; set; }
        string _fileName = "ini";
        public FileDB()
        {
            IniDB();
        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        public void IniDB()
        {
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + _fileName))
            {
                var text = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + _fileName);
                if (text != "")
                {
                    //Model = System.Text.Json.JsonSerializer.Deserialize<FileModel>(text);
                    Model = Newtonsoft.Json.JsonConvert.DeserializeObject<FileModel>(text);
                }

            }
            if (Model == null)
            {
                Model = new FileModel();
                Model.Port = 3434;
                Model.Edition = 12;
                Model.EditionTime = DateTime.Now;
                Model.IP = "192.168.1.54";
                Updata();
            }
        }
        public void Updata()
        {
            if (Model != null)
            {
                if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + _fileName))
                {
                    File.Create(AppDomain.CurrentDomain.BaseDirectory + _fileName);
                    Thread.Sleep(20);
                }
                File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + _fileName,
                    JsonConvert.SerializeObject(Model), Encoding.UTF8);
            }
        }
    }
}
